package com.xoa.service.emergency;

import com.xoa.dao.fixAssets.EduFixAssetsMapper;
import com.xoa.model.fixAssets.EduFixAssets;
import com.xoa.dao.emergency.AssetGroupMapper;
import com.xoa.model.emergency.AssetGroup;
import com.xoa.util.common.StringUtils;
import com.xoa.util.common.wrapper.BaseWrapper;
import com.xoa.util.page.PageParams;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AssetGroupService {

    @Resource
    AssetGroupMapper assetGroupMapper;

    @Resource
    EduFixAssetsMapper eduFixAssetsMapper;


    // 添加
    public BaseWrapper add(AssetGroup assetGroup){
        BaseWrapper baseWrapper = new BaseWrapper();

        int insert = assetGroupMapper.insert(assetGroup);

        if(insert>0){
            baseWrapper.setFlag(true);
            baseWrapper.setCode("0");
            baseWrapper.setMsg("添加成功");
        }
        return baseWrapper;
    }

    // 更新
    public BaseWrapper update(AssetGroup assetGroup){
        BaseWrapper baseWrapper = new BaseWrapper();

        int insert = assetGroupMapper.updateByPrimaryKeySelective(assetGroup);

        if(insert>0){
            baseWrapper.setFlag(true);
            baseWrapper.setCode("0");
            baseWrapper.setMsg("更新成功");
        }
        return baseWrapper;
    }

    // 删除
    public BaseWrapper delete(Integer groupId){
        BaseWrapper baseWrapper = new BaseWrapper();

        int insert = assetGroupMapper.deleteByPrimaryKey(groupId);

        if(insert>0){
            baseWrapper.setFlag(true);
            baseWrapper.setCode("0");
            baseWrapper.setMsg("删除成功");
        }
        return baseWrapper;
    }


    // 根据id查询单个信息
    public BaseWrapper selectById(Integer groupId,String type){
        BaseWrapper baseWrapper = new BaseWrapper();

        AssetGroup assetGroup = assetGroupMapper.selectByPrimaryKey(groupId);
        String assetIds = assetGroup.getAssetIds();
        if(!StringUtils.checkNull(assetIds)){
            StringBuilder sb = new StringBuilder();
            List<EduFixAssets> fixAssetsList = new ArrayList<EduFixAssets>();
            for (String id:assetIds.split(",")) {
                EduFixAssets eduFixAssets = eduFixAssetsMapper.selectByPrimaryKey(id);
                if(!StringUtils.checkNull(type)&&"1".equals(type)){
                    fixAssetsList.add(eduFixAssets);
                }
                sb.append(eduFixAssets.getAssetsName()).append(",");
            }
            assetGroup.setAssetNames(sb.toString());
            if("1".equals(type)){
                baseWrapper.setData(fixAssetsList);
            }else{
                baseWrapper.setData(assetGroup);
            }
        }
        baseWrapper.setFlag(true);
        baseWrapper.setCode("0");
        baseWrapper.setMsg("查询成功");

        return baseWrapper;
    }

    // 查询基础信息 id和name
    public BaseWrapper selectAllBaseInfo(Integer page,Integer limit){
        BaseWrapper baseWrapper = new BaseWrapper();
        Map<String,Object> map = new HashMap<String,Object>();
        PageParams pageParams = new PageParams();
        pageParams.setPage(page);
        pageParams.setPageSize(limit);

        map.put("page", pageParams);
        List<AssetGroup> assetGroups = assetGroupMapper.selectAllBaseInfo(map);

        baseWrapper.setData(assetGroups);
        baseWrapper.setFlag(true);
        baseWrapper.setCode("0");
        return baseWrapper;
    }


}
